package com.guilang.localmusic;

import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.guilang.localmusic.adapter.SongsAdapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class MySongs extends AppCompatActivity {

    private List<SongItem> songItemList = new ArrayList<>();
    private FrameLayout frameLayout_list, frameLayout_play;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_songs);
        ActivityCollector.addActivity(this);
        Toolbar toolbar = findViewById(R.id.songtoolbar);
        setSupportActionBar(toolbar);

        //去除标题栏得文字
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(true);
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_back);
        }
        //填充列表
        songItemList = scanAllAudioFiles();
        //获取ListView布局
        //设置adapter
        SongsAdapter adapter = new SongsAdapter(MySongs.this, R.layout.song, songItemList);
        ListView listView = findViewById(R.id.song_items);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();//刷新ListView变动


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SongItem songItem = songItemList.get(position);
                Toast.makeText(MySongs.this, "即将播放：" + songItem.getSongName(), Toast.LENGTH_SHORT).show();
                //将选中的歌曲信息传入下一个Intent
                InitSongs(position);
            }
        });
    }

    private List<SongItem> scanAllAudioFiles() {
        List<SongItem> mylist = new ArrayList<>();
        //查询手机中所有的音频文件
        Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null, null);
        if (cursor.moveToFirst()) { //跳转至查询的第一个item
            while (!cursor.isAfterLast()) {
                int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));    //ID
                String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)); //歌曲名
                String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));//专辑图片
                String singer = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));//歌手
                String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));//绝对路径
                int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));//时长
                Long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)); //文件大小
                //跳过小于800K的文件
                if (size > 1024 * 800) {
                    SongItem song = new SongItem(title, singer, R.mipmap.avater, url, duration);
                    mylist.add(song);
                }
                cursor.moveToNext();
            }
            cursor.close();//释放资源
        }
        return mylist;
    }

    private void InitSongs(int position) {
        //ListView中选项被选中
        Intent intent = new Intent(MySongs.this, MusicPlayer.class);
        intent.putExtra("songlist", (Serializable) songItemList);
        intent.putExtra("Sposition", position);
        intent.putExtra("Sname", songItemList.get(position).getSongName());
        intent.putExtra("Ssinger", songItemList.get(position).getSinger());
        intent.putExtra("Simage", songItemList.get(position).getSongImage());
        intent.putExtra("Surl", songItemList.get(position).getSongUrl());
        intent.putExtra("Sduration", songItemList.get(position).getSongDuration());
        startActivity(intent);
    }

    //ToolBar上的搜索按钮
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_my_songs, menu);
        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.ic_search).getActionView();
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));
        return true;
    }

    //ToolBar左边的按钮作用
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
            default:
                break;
        }
        return true;
    }
}
